package com.example.imoocmusicdemo.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.RegexUtils;
import com.example.imoocmusicdemo.R;
import com.example.imoocmusicdemo.helps.DBOpenHelper;

public class RegisterActivity extends BaseActivity {

    Button btn;
    EditText num,pwd,pwd2;
    private DBOpenHelper mDBOpenHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
        mDBOpenHelper = new DBOpenHelper(this);

        pwd2 = findViewById(R.id.password2);
        btn = findViewById(R.id.Btn);
        num = findViewById(R.id.number);
        pwd = findViewById(R.id.password);



        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String number = num.getText().toString().trim();
                String password = pwd.getText().toString().trim();
                String password2 = pwd2.getText().toString().trim();
                if (!RegexUtils.isMobileExact(number)) {
                    Toast.makeText(getApplicationContext(), "无效手机号码", Toast.LENGTH_SHORT).show();
                    if (TextUtils.isEmpty(number)){
                        Toast.makeText(getApplicationContext(),"请输入手机号码",Toast.LENGTH_SHORT).show();
                    }
                }else if (TextUtils.isEmpty(password)) {
                    Toast.makeText(getApplicationContext(),"请设置密码",Toast.LENGTH_SHORT).show();
                }else if (TextUtils.isEmpty(password2)) {
                    Toast.makeText(getApplicationContext(),"请确认密码",Toast.LENGTH_SHORT).show();
                }else {
                    if (password.equals(password2)) {
                        //将用户名和密码加入到数据库中
                        mDBOpenHelper.add(number, password2);
                        Intent intent1 = new Intent(RegisterActivity.this, LoginActivity.class);
                        startActivity(intent1);
                        finish();
                        Toast.makeText(getApplicationContext(), "验证通过，注册成功", Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(getApplicationContext(), "两次密码不一致,注册失败", Toast.LENGTH_SHORT).show();
                    }
                    Toast.makeText(getApplicationContext(),"注册成功,已为您跳转至登录界面，请您登录",Toast.LENGTH_LONG).show();
                    Log.i("LoginActivity","登录的用户信息："+"手机号码："+number+"，密码："+password);
                    Intent intent = new Intent("android.intent.action.LoginActivity");
                    startActivityForResult(intent,111);
                }
            }

        });

    }

    private  void initView () {
        initNavBar(true,"成为emo伙伴吧", false);
    }
}